<?php

class ChucNangSanPhamController extends Zend_Controller_Action
{
	
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function newAction()
    {
        // action body
    }

    public function sucessNewAction()
    {
        // action body
        $tenChucNang=$this->_request->getParam('tenChucNang');
        $moTa=$this->_request->getParam('moTa');
        //luu file len 
        $name=Application_Model_util_MyImageUtil::makeUpload('sanPham/chucNang',$this->getRequest(),64,64);
        
        //goi service
        $service=Application_Model_service_ChucNangSanPhamService::getInstance();
        $id=$service->taoChucNangSanPham($tenChucNang,$moTa,$name[0]);
        //add view
        $this->view->id=$id;
    }

    public function updateAction()
    {
        // action body
       //lay du lieu muon sua
       $id=$this->_request->getParam('id');
       $sv=Application_Model_service_ChucNangSanPhamService::getInstance();
       $chucNang=$sv->getOneChucNangSanPham($id);
       //add view
       $this->view->chucNang=$chucNang;
       $this->view->id=$id;
    }

    public function sucessUpdateAction()
    {
        // action body
       $tenChucNang=$this->_request->getParam('tenChucNang');
       $moTa=$this->_request->getParam('moTa');
       $id=$this->_request->getParam('id');
       $hinhIcon=$this->_request->getParam('hinhIcon');
       //luu file len 
       
       (array)$name=Application_Model_util_MyImageUtil::makeUpload('sanPham/chucNang',$this->getRequest(),64,64);
                		if(sizeof($name)==0){
                			$name[0]=$hinhIcon;
                		} else{
                			//xoa hinh cu
                			$uploadDir= realpath(APPLICATION_PATH . '/../public/images/sanPham/chucNang') ;
                			$file=$uploadDir.'/'.$hinhIcon;
                			if(file_exists($file)){
                				unlink($file);
                			}
                			
                		}
       //goi serivce
       
       $service=Application_Model_service_ChucNangSanPhamService::getInstance();
       $result=$service->suaChucNangSanPham($id,$tenChucNang, $moTa, $name[0]);
       
       //add view
       $this->view->result=$result;
    }

    public function getAllAction()
    {
        //goi service
   	$service=Application_Model_service_ChucNangSanPhamService::getInstance();
   	$resutl=$service->loadAllChucNangSanPham();
   	//add view
   	$this->view->list=$resutl;
    }

    public function taoItemAction()
    {
        // action body
       //lay danh sach chuc nang
       $sv=Application_Model_service_ChucNangSanPhamService::getInstance();
       $resutl=$sv->loadAllChucNangSanPham();
       //add view
       $this->view->list=$resutl;
    }

    public function sucessTaoItemAction()
    {
        // action body
       $idNhomChucNang=$this->_request->getParam('id');
       $noiDung=$this->_request->getParam('noiDung');
       
       //goi service
       $sv=Application_Model_service_ChucNangSanPhamService::getInstance();
       $result=$sv->taoItemChucNangSanPham($idNhomChucNang, $noiDung);
       
       //add view
       $this->view->id=$result;
    }

    public function updateItemAction()
    {
        // action body
       $id=$this->_request->getParam('id');
       
       //goi service
       $sv=Application_Model_service_ChucNangSanPhamService::getInstance();
       
       //lai item de update
       $result=$sv->getOneItemChucNangSanPham($id);
       //lay danh sach nhom chuc nang
       $listNhomChucNang=$sv->loadAllChucNangSanPham();
       //add view 
       $this->view->item=$result;
       $this->view->id=$id;
       $this->view->list=$listNhomChucNang;
    }

    public function successUpdateItemAction()
    {
        // action body
       $id=$this->_request->getParam('id');
       $idNhomChucNang=$this->_request->getParam('idNhomChucNang');
       $noiDung=$this->_request->getParam('noiDung');
       
       //goi service
       $service=Application_Model_service_ChucNangSanPhamService::getInstance();
       $result=$service->suaItemChucNangSanPham($id, $idNhomChucNang, $noiDung);//tra ve boolean
       //add view
       $this->view->result=$result;
    }

    public function xoaChucNangSanPhamAction()
    {
        // action body
        $id=$this->_request->getParam('id');
        //goi service
        $service=Application_Model_service_ChucNangSanPhamService::getInstance();
        $result=$service->xoaChucNangSanPham($id);//tra ve 1 hoặc Limit nếu id=1
        //add view
       $this->view->result=$result;
    }

    public function xoaItemChucNangSanPhamAction()
    {
        // action body
        $id=$this->_request->getParam('id');
        //goi service
        $service=Application_Model_service_ChucNangSanPhamService::getInstance();
        $result=$service->xoaItemChucNangSanPham($id);//tra ve boolean
        //add view
       $this->view->result=$result;
    }


}



















